﻿XXXX.usermanage = {

	init : function() {
		// alert(extjsbase.LoginUser);
		var sm = new Ext.grid.CheckboxSelectionModel( {
		// singleSelect : true
				});

		var colM = new Ext.grid.ColumnModel(
				[
						new Ext.grid.RowNumberer(),
						sm,
						{
							header : '姓名',
							dataIndex : 'sname',
							width : 80,
							sortable : true
						},
						{
							header : '部门',
							dataIndex : 'sdepartmentname',
							width : 80,
							sortable : true
						},
						{
							header : '级别',
							dataIndex : 'srankname',
							width : 100,
							sortable : true
						},
						{
							renderer : function(value) {
								return extjsbase.convert('isex', value);
							},
							header : '性别',
							dataIndex : 'isex',
							width : 35,
							sortable : true

						},
						{
							header : '电话',
							dataIndex : 'sphone',
							width : 80,
							sortable : true

						},
						{
							renderer : function(value, metadata, record,
									rowIndex, colIndex, store) {

								return '<a href="javascript:XXXX.usermanage.onUpdateClick(' + rowIndex + ');"><img title="修改用户信息" src="img/application/application_form_edit.png"></a>';
							},
							header : '修改',
							dataIndex : 'gridoperation',
							sortable : false,
							width : 40
						},
						{
							renderer : function(value, metadata, record,
									rowIndex, colIndex, store) {

								return '<a href="javascript:XXXX.usermanage.onLookClick(' + rowIndex + ');"><img title="查看详细信息" src="img/application/application_view_detail.png"></a>';
							},
							header : '查看',
							dataIndex : 'gridoperation',
							sortable : false,
							width : 40
						}

				]);
		colM.defaultSortable = false;
		// var loginuser = extjsbase.LoginUser;
		var store = new Ext.data.Store(
				{
					// baseParams : query ? arrs : {},
					proxy : new Ext.data.HttpProxy(
							{
								url: './js/usermanage/user.json'
								// url : 'ExtControllerServlet?requestType=grid&action=usersAction.findAll'// &UserID='+loginuser
							}),
					reader : new Ext.data.JsonReader( {
						totalProperty : 'totalCount',
						root : 'data'
					}, [ {
						name : 'id'
					}, {
						name : 'sname'
					}, {
						name : 'spassword'
					}, {
						name : 'sdepartmentname'
					}, {
						name : 'srankname'
					}, {
						name : 'semailaddr'
					}, {
						name : 'dbirthday'
					}, {
						name : 'isex'
					}, {
						name : 'spostaladdr'
					}, {
						name : 'spostalcode'
					}, {
						name : 'sofficeno'
					}, {
						name : 'sphone'
					}, {
						name : 'sfax'
					}, {
						name : 'sposition'
					}, {
						name : 'istate'
					}, {
						name : 'dcrtdate'
					}, {
						name : 'sremark'
					}, {
						name : 'sipaddr'
					} ])
				});
		store.load( {
			params : {
				start : 0,
				limit : 20
			}
		});

		var tab = Ext.getCmp('tabView');
		var grid = new Ext.grid.GridPanel( {
			renderTo : 'form_usermanage',
			id : 'gridusermanage',
			height : tab.getInnerHeight(),
			width : tab.getInnerWidth(),
			cm : colM,
			sm : sm,
			store : store,
			tbar : [ {
				text : '新建',
				iconCls : 'add_user',
				tooltip : '新建用户',
				handler : XXXX.usermanage.onLoadClick
			}, '-', {
				text : '删除',
				handler : XXXX.usermanage.onDeleteClick,
				iconCls : 'delete_user',
				tooltip : '删除选中用户'
			}, '-', {
				text : '查询',
				iconCls : 'magnify',
				tooltip : '查询特定用户',
				handler : XXXX.usermanage.onSearchClick
			}, '-', {
				text : '导出列表',
				iconCls : 'export',
				tooltip : '导出列表信息到Excel',
				handler : function() {
				}
			}, '-', {
				text : '授权',
				iconCls : 'key',
				tooltip : '授予用户角色权限',
				handler : function() {
				}
			} ],
			// 定义分页
			bbar : System.pagingBar(store)
		});
	},

	// 查找
	createSearchForm : function(grid) {
		return extjsbase.forms.create('usermanage_search', {
			grid : grid
		});
	},

	onSearchClick : function(item) {
		var grid = Ext.getCmp('gridusermanage');
		var panel1 = XXXX.usermanage.createSearchForm(grid);
		popWin( {
			title : '查询条件输入',
			items : panel1
		});
	},

	// 更新信息
	createonUpdateForm : function(record, grid) {
		var simple = extjsbase.forms
				.create(
						'usermanage_base',
						{
							grid : grid,
							url : 'ExtControllerServlet?requestType=form&action=usersAction.updateUser&id=' + record
									.get("id")
						}, {
							record : record
						});

		return simple;
	},
	onUpdateClick : function(rowIndex) {
		var grid = Ext.getCmp('gridusermanage');
		var record = grid.getStore().getAt(rowIndex);
		var panel1 = XXXX.usermanage.createonUpdateForm(record, grid);
		// panel1.render();
	popWin( {
		height : 300,
		title : '修改用户信息',
		items : panel1
	});
},
// 新建
	createUpLoadForm : function(grid) {
		var simple = extjsbase.forms
				.create(
						'usermanage_base',
						{
							grid : grid,
							url : 'ExtControllerServlet?requestType=form&action=usersAction.createUser'
						}, {});
		// alert(simple.url);
		return simple;
	},

	// new
	onLoadClick : function(item) {
		var ct = Ext.getCmp('gridusermanage');
		var panel1 = XXXX.usermanage.createUpLoadForm(ct);
		popWin( {
			title : '新建用户',
			height : 300,
			width : 360,
			items : panel1
		});
	},

	// delete
	onDeleteClick : function(item) {
		// alert((item.query));
		var grid = Ext.getCmp('gridusermanage');
		var selects = grid.getSelectionModel().getSelections();
		if (selects.length <= 0) {
			Ext.MessageBox.alert("系统提示", "请选择要删除的用户!");
		}

		if (selects.length > 0) {
			Ext.MessageBox
					.confirm("系统提示",
							"请确认是否删除选中用户",
							function(button, text) {
								if (button == "yes") {

									var deleteStr = "";
									for (i = 0; i < selects.length; i++) {
										if (deleteStr != "") {
											deleteStr += ",";
										}
										deleteStr += selects[i].get('id');
										// alert(deleteStr);
						}

						Ext.Ajax
								.request( {
									url : 'ExtControllerServlet?requestType=form&action=usersAction.deleteUser&id=' + deleteStr,
									success : function(action, request) {
										// debugger;
									// alert(action);//alert(Ext.encode(request));

									var obj = Ext.decode(action.responseText);
									grid.getStore().reload();
									Ext.MessageBox.alert("删除成功！", obj.message);

								},
								failure : function(action, request) {

									var obj = Ext.decode(action.responseText);
									grid.getStore().reload();
									Ext.MessageBox.alert("删除失败！", obj.message);
								}
								})
					}
				}	);

		}
	},
	// delete end

	onLookClick : function(rowIndex) {

		var grid = Ext.getCmp('gridusermanage');
		var record = grid.getStore().getAt(rowIndex);
		var simple = extjsbase.forms.create('usermanageLook', {}, {
			record : record
		});
		popWin( {
			height : 300,
			title : '用户详细信息',
			items : simple
		});
	}
}

System.register("usermanage", XXXX.usermanage);